<?php
namespace Qy {
    /**
    * 企业微信成员管理类
    * @author Jamers
    * @since 2017.1.1
    * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
    */
    class User {
        private $common;
        public function __construct($ary = array()) {
            if (isset($ary['common'])) {
                $this->common = &$ary['common'];
            }else{
                $this->common = new \Qy\Common();
            }
        }
        /**
        * 成员关注企业号二次验证
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E6%88%90%E5%91%98%E5%85%B3%E6%B3%A8%E4%BC%81%E4%B8%9A%E5%8F%B7
        * 
        * @param mixed $userid
        * @param AccessToken $a_token
        */
        public function authsucc($userid,$a_token='') {
            if (empty($userid)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/authsucc?access_token={$a_token}&userid={$userid}";
            return $this->common->httpRequest($url);
        }
        /**
        * 创建成员
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function createUser($ary=array(),$a_token='') {
            if (empty($ary)) return false;
            if (!isset($ary['userid']) || !isset($ary['name']) || !isset($ary['department'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 更新成员信息
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function updateUser($ary=array(),$a_token='') {
            if (empty($ary)) return false;
            if (!isset($ary['userid'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }

        /**
        * 删除成员
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function deleteUser($userid,$a_token='') {
            if (empty($userid)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token={$a_token}&userid={$userid}";
            return $this->common->httpRequest($url);
        }
        
        /**
        * 批量删除成员
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function batchdeleteUser($ary=array(),$a_token='') {
            if (empty($ary)) return false;
            if (!isset($ary['useridlist'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 获取成员信息
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
        * 
        * @param mixed $userid
        * @param mixed $a_token
        */
        public function getUserbyUserid($userid,$a_token='') {
            if (empty($userid)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={$a_token}&userid={$userid}";
            return $this->common->httpRequest($url);
        }
        
        /**
        * 获取部门成员简要列表
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
        * 
        * @param mixed $department_id
        * @param mixed $a_token
        * @param mixed $fetch_child
        * @param mixed $status
        */
        public function getUserSimpleList($department_id,$a_token='',$fetch_child=0,$status=0) {
            if (empty($department_id)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token={$a_token}&department_id={$department_id}&fetch_child={$fetch_child}&status={$status}";
            return $this->common->httpRequest($url);
        }
        
        /**
        * 获取部门成员详情列表
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98
        * 
        * @param mixed $department_id
        * @param mixed $a_token
        * @param mixed $fetch_child
        * @param mixed $status
        */
        public function getUserList($department_id,$a_token='',$fetch_child=0,$status=0) {
            if (empty($department_id)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={$a_token}&department_id={$department_id}&fetch_child={$fetch_child}&status={$status}";
            return $this->common->httpRequest($url);
        }
    }
}
?>
